package com.ly.mpayment.persist.service.mobile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.Mobile;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;


@Service
public class MobileServiceImpl extends AbstractService<Mobile, Serializable>  implements IMobileService {

	@Override
	public Mobile findPhone(String phone) {
		return this.findUnique("select m from Mobile m where m.phone = ?", phone);
	}

	@Override
	public boolean saveMobile(Mobile weiliu) {
		if(weiliu != null){
			this.saveOrUpdate(weiliu);
			return true;
		}
		return false;
	}

	@Override
	public List<Mobile> findAllProvince() {
		return this.find("select distinct m.province from Mobile m ");
	}

	@Override
	public List<Mobile> findAllCity(String province) {
		return this.find("select DISTINCT m.city from Mobile m where m.province =? ",province);
	}

	@Override
	public boolean updateMobile(Mobile weiliu) {
		if(weiliu != null){
			this.updateEntity(weiliu);
			return true;
		}
		return false;
	}

	@Override
	public String findByMaxId() {
		return this.getSession().createSQLQuery("SELECT id from mobile order by id  desc limit 1").uniqueResult().toString();
	}

	@Override
	public boolean createMobile(Mobile mobile) {
		String sql = "INSERT INTO mobile (`id`, `phone`, `province`, `city`, `service_provider`, `city_code`, `postcode`, `state`) VALUES " +
				" ("+mobile.getId()+","+mobile.getPhone()+",'"+mobile.getProvince()+"', '"+mobile.getCity()+"', '"+mobile.getServiceProvider()+"', "+mobile.getCityCode()+","+mobile.getPostCode()+", 0);";
		return this.getSession().createSQLQuery(sql).executeUpdate() > 0;
	}

	/**
	 * private String phone;
		private String province;
		private String city;
	 */
	@Override
	public Page<Mobile> findPageMobile(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select m from Mobile m where 1=1 #[ and m.phone like :phone]#  #[ and m.city = :city]#  #[ and m.province = :province]#   #[ and m.state = :state]# order by m.city asc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public Mobile findById(long id) {
		return this.findUnique("select m from Mobile m where m.id = ?", id);
	}

	@Override
	public List<Mobile> findProvinceCity(String province) {
		List  lists = this.find("select DISTINCT m.city,m.province from Mobile m where m.province =? ",province);
		List<Mobile> mobiles = new ArrayList<>();
		for(int i = 0 ; i < lists.size() ; i++){
			Mobile mobile = new Mobile();
			Object [] obj = (Object[]) lists.get(i);
			if(obj != null){
				mobile.setCity(obj[0] == null ? "": obj[0].toString());
				mobile.setProvince(obj[1].toString());
				mobiles.add(mobile);
			}
		}
		return mobiles;
	}

	@Override
	public List<Mobile> findAllCityIsnull(boolean temp) {
		String sql = temp ? "select m from Mobile m where m.city is null" : "select m from Mobile m where m.city = ''";
		return this.find(sql);
	}


}
